package com.usian.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.usian.pojos.Employee;
import com.usian.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

    @GetMapping("/findEmployee")
    public String findList(Model model,HttpSession session){

        Employee employee = (Employee)session.getAttribute("employee");

        QueryWrapper<Employee> wrapper = new QueryWrapper<>();

        wrapper.lambda().eq(Employee::getEname,employee.getEname());

        Employee one = employeeService.getOne(wrapper);

        List<Employee> employee1 = employeeService.findEmployee(one);

        model.addAttribute("list",employee1);

        model.addAttribute("department", employeeService.findDList());

        model.addAttribute("post",employeeService.findPList());

        return "employee";
    }

    @GetMapping("/toadd")
    public String toadd(Model model,HttpSession session){

        Employee employee = (Employee)session.getAttribute("employee");

        QueryWrapper<Employee> wrapper = new QueryWrapper<>();

        wrapper.lambda().eq(Employee::getEname,employee.getEname());

        if (employeeService.getOne(wrapper).getQuanxian().equals("1")||employeeService.getOne(wrapper).getQuanxian().equals("2")){

            model.addAttribute("department", employeeService.findDList());

            model.addAttribute("post",employeeService.findPList());

            return "add";
        }

        return "login";
    }

    @GetMapping("/findEmployeeById")
    public String toupdate(Model model,HttpSession session,Integer id){

        Employee employee = (Employee)session.getAttribute("employee");

        QueryWrapper<Employee> wrapper = new QueryWrapper<>();

        wrapper.lambda().eq(Employee::getEname,employee.getEname());

        if (employeeService.getOne(wrapper).getQuanxian().equals("1")||employeeService.getOne(wrapper).getQuanxian().equals("2")){

            model.addAttribute("department", employeeService.findDList());

            model.addAttribute("post",employeeService.findPList());

            model.addAttribute("e",employeeService.getById(id));

            return "update";
        }

        return "login";
    }

    @PostMapping("/addEmployee")
    public String addEmployee(Employee employee){

        employeeService.addEmployee(employee);

        return "redirect:/employee/findEmployee";
    }

    @PostMapping("/updateEmployee")
    public String updateEmployee( Employee employee){

        employeeService.updateEmployee(employee);

        return "redirect:/employee/findEmployee";
    }

    @GetMapping("/delEmployee")
    public String delEmployee(HttpSession session,Model model,Integer id){

        Employee employee = (Employee)session.getAttribute("employee");

        QueryWrapper<Employee> wrapper = new QueryWrapper<>();

        wrapper.lambda().eq(Employee::getEname,employee.getEname());

        if (employeeService.getOne(wrapper).getQuanxian().equals("1")||employeeService.getOne(wrapper).getQuanxian().equals("2")){

            model.addAttribute("department", employeeService.findDList());

            model.addAttribute("post",employeeService.findPList());

            employeeService.delEmployee(id);

            return "redirect:/employee/findEmployee";
        }
        return "login";
    }

    @GetMapping("/login")
    public String login(){

        return "login";
    }

    @PostMapping("/tologin")
    public String tologin(Employee employee, HttpSession session){

        int login = employeeService.login(employee);

        if (login == 0){

            session.setAttribute("employee",employee);

            return "redirect:/employee/findEmployee";
        }else {

            return "login";
        }
    }
}
